Typical HAL SRAM use cases for migration

This section provides information about typical HAL SRAM use cases for migration.

Note

HAL2, HAL SRAM do only the FMC configuration to access the external memory, No more processing (Read/Write) APIs anymore. Once the FMC is configured, SRAM become memory mapping and accessible for CPU and other master. To checkout the different cases of configuration and how to use them see HAL FMC SRAM initialization and configuration functions migration.

Use case: Read and write operations

Sequence diagram

Read and write operations

@startuml

== Prerequisite: Initialization and configuration ==

== Process ==

"User Application" -> "HAL SRAM Driver" : HAL_SRAM_Write_xx()
note right: **Write operation**
"HAL SRAM Driver" --> "User Application": hal status : HAL_OK or HAL_ERROR
note right: **Return**

"User Application" -> "HAL SRAM Driver" : HAL_SRAM_Read_xx()
note right: **Read operation**
"HAL SRAM Driver" --> "User Application" : hal status : HAL_OK or HAL_ERROR
note right: **Return**

@enduml

Code snippet

HAL1

HAL2

/* - */



// Write data to the SRAM memory
HAL_SRAM_Write_8b(&hsram,
                   (uint32_t *)(target_address),
                   data_to_write,
                   BufferSize
                  );

// Read data from the SRAM memory
HAL_SRAM_Read_8b(&hsram,
                  (uint32_t *)(target_address),
                  data_read,
                  BufferSize
                 );
uint32_t size;
uint8_t *psramaddress = (uint8_t *)target_address;
uint8_t *psrcbuff = data_to_write;
uint8_t *pdestbuff = data_read;
/* Write data to memory */
for (size = BufferSize; size != 0U; size--)
{
   *psramaddress = *psrcbuff;
   psrcbuff++;
   psramaddress++;
}
/* Read data from memory */
for (size = BufferSize; size != 0U; size--)
{
   *pdestbuff = *psramaddress;
   pdestbuff++;
   psramaddress++;
}

Key changes

  1. In HAL2, the SRAM driver only configures FMC; there are no dedicated SRAM read/write APIs.

  2. Once FMC is configured, SRAM is memory-mapped and accessed by direct CPU reads/writes.


Use case: Read and write DMA operations

Sequence diagram

Read and write DMA operations

@startuml

== Prerequisite: Initialization and configuration ==

== Process ==

"User Application" -> "HAL SRAM Driver" : HAL_SRAM_Write_DMA()
note right: **Write DMA operation**
"HAL SRAM Driver" --> "User Application": hal status : HAL_OK or HAL_ERROR
note right: **Return**

"User Application" -> "HAL SRAM Driver" : HAL_SRAM_Read_DMA()
note right: **Read DMA operation**
"HAL SRAM Driver" --> "User Application" : hal status : HAL_OK or HAL_ERROR
note right: **Return**

@enduml

Code snippet

HAL1

HAL2

/* Write data to the SRAM memory -------------------- */
HAL_SRAM_Write_DMA(&hsram,
                   (uint32_t *)(target_address),
                   data_to_write,
                   BufferSize
                 );

/* Read data from the SRAM memory */
HAL_SRAM_Read_DMA(&hsram,
                  (uint32_t *)(target_address),
                  data_read,
                  BufferSize
                );
/* Write data to memory */
HAL_DMA_StartDirectXfer_IT(&dma_handle,
                           data_to_write,
                           (uint32_t *)(target_address),
                           BufferSize
                          );

/* Read data from memory */
HAL_DMA_StartDirectXfer_IT(&dma_handle,
                           (uint32_t *)(target_address),
                           data_read,
                           BufferSize
                          );

Key changes

  1. In HAL2, SRAM read/write is done through memory mapping; DMA transfers use direct DMA APIs.

  2. HAL1 uses HAL_SRAM_Read_DMA()/HAL_SRAM_Write_DMA() while HAL2 uses HAL_DMA_StartDirectXfer_IT().


Use case: Disable unused memory

Note

When external SRAM is not needed, it is possible to disable HAL SRAM to save power.

Code snippet

HAL1

HAL2

The only option to disable SRAM is to deinitialize the HAL SRAM driver. When SRAM is needed again, the initialization sequence must be performed again.

It is possible to disable HAL SRAM without full deinitialization when SRAM is no longer needed, and to re-enable it when the external memory is required again.

@startuml

== Prerequisite: Initialization and configuration ==

== Process ==

note over "User Application": **Perform any access operation**

"User Application" -> "HAL SRAM Driver" : HAL_SRAM_DeInit()
note right
   **Deinitialize the HAL SRAM driver**
   **to disable the HAL SRAM Memory**
   **to save Power**
end note
"User Application"->"HAL SRAM Driver" : HAL_SRAM_Init()
note right
   **Reinitialize the HAL SRAM driver**
   **to enable the HAL SRAM Memory**
   **to reuse SRAM Driver**
end note

note over "User Application": **Perform any access operation**

@enduml

@startuml

==Prerequisite: Initialization and configuration ==

== Process ==

note over "User Application": **Perform any access operation**

"User Application"->"HAL SRAM Driver" : HAL_SRAM_DisableMemory()
note right
   **Disable the HAL SRAM memory**
   **to save Power**
end note

"User Application"->"HAL SRAM Driver" : HAL_SRAM_EnableMemory()
note right
   **Enable the HAL SRAM memory**
   **to reuse SRAM Driver**
end note

note over "User Application": **Perform any access operation**

@enduml

/* Perform any access operation */

/* Deinitialize the SRAM */
HAL_SRAM_DeInit(&hsram);
/* Reinitialize the SRAM */
HAL_SRAM_Init(&hsram, &Sram_Timing, NULL);

/* Perform any access operation */
/* Perform any access operation */

/* Disable an SRAM sub-bank to save power */
HAL_SRAM_DisableMemory(&hsram);
/* Enable the corresponding SRAM sub-bank */
HAL_SRAM_EnableMemory(&hsram);

/* Perform any access operation */

Key changes

  1. In HAL2, SRAM can be disabled/enabled without full deinitialization.

  2. In HAL1, disabling SRAM requires deinitializing and reinitializing the driver.


Use case: Disable and enable write protection

Note

The HAL1 API name for disabling write operations has been renamed to write protection enable in HAL2.

Sequence diagram

Disable and enable write protection

@startuml

== Prerequisite: Initialization and configuration ==

== Process ==

"User Application" -> "HAL SRAM Driver" : HAL_SRAM_WriteOperation_Enable()
note over "HAL SRAM Driver"
   **Enable write operation**
   **to perform write operations**
end note

"User Application" -> "HAL SRAM Driver" : Write operations

"User Application" -> "HAL SRAM Driver" : HAL_SRAM_WriteOperation_Disable()
note over "HAL SRAM Driver"
   **Disable write operation**
   **to prevent any write operation**
end note

@enduml

Code snippet

HAL1

HAL2

/* Enable write operation */
HAL_SRAM_WriteOperation_Enable(&hsram);

/* Do some write operations if needed */

/* Disable write operation */
HAL_SRAM_WriteOperation_Disable(&hsram);
/* Disable the Write protection */
HAL_SRAM_DisableWriteProtection(&hsram);

/* Do some write operations if needed */

/* Enable the Write protection */
HAL_SRAM_EnableWriteProtection(&hsram);

Key changes

  1. HAL2 uses explicit write protection APIs (enable/disable write protection).

  2. HAL1 enables/disables write operations using HAL_SRAM_WriteOperation_Enable()/HAL_SRAM_WriteOperation_Disable().

Related examples in HAL2

Example name

Brief

example_hal_fmc_sram_write_protection

Enabling FMC SRAM write protection and triggering a hard fault in case of an erroneous write access.